<?php
class ExpressFee{
    protected $main_table = 'data_express_fee';
	protected $main_id = 'fee_id';
	protected $db = null;
	
	public function __construct($db){
		$this->db=$db;
	}
   
	public function __destruct(){
		unset($this->db);
	}
	public function get_table_name(){
	    return $this->main_table;
	    
	}
	/**
	* 	insert ExpressFee
	* 	@param array $data 
	* 	@return int affected count
	*/
	public function insert_data($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$rows = $this->db->insert($this->main_table, $data);
		$insert_id = $this->db->getInsertId();
		$this->update_data_file();
		return $insert_id;	
	}
	
	/**
	* 	update ExpressFee
	* 	@param array $data 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function update_data($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$result = $this->db->update($this->main_table,$data,$where);
		$this->update_data_file();
		return $result;
	}
	
	/**
	* 	delete ExpressFee
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function del_data($where){
		$result = $this->db->delete($this->main_table,$where);
		$this->update_data_file();
		return $result;
	}
	

	
	
	/**
	* 	get ExpressFee info
	* 	@param int $id 
	*	@param array/string $field
	* 	@return array one row data
	*/
	public function get_data_row($id,$field='*'){
		if(!intval($id))
		my_jump('输入的参数有误!');
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$this->main_id."='".intval($id)."' ";
		$data = $this->db->getRow($sql);
		return $data;
	}
	
	
	/**
	* 	get ExpressFee array
	* 	@param string $where
	* 	@return array data
	*/
	public function get_data_array($where,$order='fee_id DESC',$limit='10',$field='*'){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where." ORDER BY ".$order;
		if($limit){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getAll($sql);
		return $data;
	}  	
	
	public function update_data_file(){
		$data = $this->get_data_array("1",'fee_id DESC','','fee_id,fee_title');		
		if($data){
			$temp_ary = array();
			foreach($data as $k=>$v){
				$temp_ary[$v['fee_id']] = $v['fee_title'];
			}			
			$new_array = '$cf_expressfee=';
			$new_array .= var_export($temp_ary,true);
			write_file(INFO_PATH.'cf_expressfee.php',$new_array,"php");		
		}
		
	}
	
	public function get_fee($fid,$count,$weight=0,$proid=0){
		if(!$fid || !$count){//参数不正确的，返回0
			return 0; 
		}
		$row = $this->get_data_row($fid);//获取邮费模板的信息						
		if($row){
			if($row['fee_is_free']){//如果免邮费的，直接返回0
				return 0; 
			}
			$fee_item = unserialize($row['fee_item']);
			if($proid){//如果指定省份，加载省份邮费
				$proid_str = ','.$proid.',';
				$is_find = 0;//是否找到相应省份的邮费标识
				foreach($fee_item['list'] as $v){//查找省份对应的邮费
					$rs = strstr(','.$v['express_areas'].',',$proid_str);
					if($rs){//如果找到对应的省份的邮费模板
						$fee_ary = $v;//计算邮费的模板
						$is_find = 1;
						break;
					}							
				}
				if(!$is_find){
					$fee_ary = $fee_item['default'];//计算邮费的模板
				}				
				
			}else{//没有指定省份，加载默认邮费
				$fee_ary = $fee_item['default'];//计算邮费的模板
			}		
		
			if($row['fee_type'] == '0'){//计费方式 0是按件数 1是按重量
											
				//根据邮费模板，计算邮费 购买的数量 减去 邮费模板首件个数
				$cut_count = $count - intval($fee_ary['express_start']);//判断是否超过首件个数
				if($cut_count > 0){//如果购买的数量超过首件				
					$fee = $fee_ary['express_postage']+ ceil($cut_count/intval($fee_ary['express_plus']))*intval($fee_ary['express_postageplus']);	
				}else{//如果购买的数量没有超过首件个 
					$fee = $fee_ary['express_postage'];	
				}				
					
			}else{//按照重量计算
				//根据邮费模板，计算邮费 购买的数量 减去 邮费模板首件个数
				$cut_weight = $weight - intval($fee_ary['express_start']);//判断是否超过首件个数
				if($cut_weight > 0){//如果购买的数量超过首件 首重运费+续重运费 				
					$fee = $fee_ary['express_postage']+ ($cut_weight/intval($fee_ary['express_plus']))*intval($fee_ary['express_postageplus']);	
				}else{//如果购买的数量没有超过首件个 
					$fee = $fee_ary['express_postage'];	
				}
				
			}			
			return $fee;
			
		}else{
		 return 0; 	
		}
		
	} 


}

?>